<?php

namespace app\mobileapp\controller;

use app\mobileapp\controller\Base;
use think\Error;
use think\facade\Cache;
use service\SmsService;
use think\Db;

class Content extends Base
{

    /**
     * User: Simony
     * Function:获取内容类型
     */
    public function getContentType()
    {
        $id = input('type/d', 0);
        $contentTypeModel = model('common/content/ContentType');
        $res = $contentTypeModel->getInfoByID($id, 'id,name,price');

        return $res ? $this->api_ajaxReturn(SUCCESS, '获取成功', $res) : $this->api_ajaxReturn(ERROR, '类型不存在');
    }


    /**
     * User: Simony
     * Function: 类型子列表
     */
    public function getContentTypeChildList()
    {
        //model
        $contentTypeModel = model('common/content/ContentType');

        $pid = input('id/d', 0);

        //where
        $where = [
            'is_show' => 1,
            'pid' => $pid
        ];

        //getData
        $list = $contentTypeModel->getList($where, 0, 'id,name,price', 'order desc');

        $this->api_ajaxReturn(SUCCESS, '获取成功', $list);
    }

    /**
     * User: Simony
     * Function:发布动态
     * @return array
     */
    public function publishDynamic()
    {
        $memberId = $this->memberId;
        $content = input('content/s', '');
        $comment_status = input('comment_status/d');
        $same_sex_status = input('same_sex_status/d');
        $imgs = input('imgs/s');

        $current_time = time();
        $data = [
            'type' => 1,
            'content' => $content,
            'comment_status' => $comment_status,
            'same_sex_status' => $same_sex_status,
            'create_time' => $current_time,
            'member_id' => $memberId
        ];

        Db::startTrans();
        $res = model('common/content/ContentLogic', 'logic')->publishContent($memberId, $data, $same_sex_status, 1);
        if ($res['code'] != SUCCESS) {
            Db::rollback();
            $this->api_ajaxReturn($res['code'], $res['msg'], $res['result']);
        }


        $dataAll = [];
        $img_arr = $imgs ? explode(',', $imgs) : [];

        if($img_arr){
            foreach ($img_arr as $v) {
                $dataAll[] = [
                    'member_id' => $memberId,
                    'table_id' => $res['result']['id'],
                    'type' => 5,
                    'status' => 1,
                    'url' => $v,
                    'create_time' => $current_time,
                ];
            }
            $add_res = $dataAll ? model('common/member/File')->addAll($dataAll) : true;
            if (!$add_res) {
                Db::rollback();
                $this->api_ajaxReturn(ERROR, '图片上传异常');
            }
        }

        Db::commit();
        $this->api_ajaxReturn($res['code'], $res['msg'], $res['result']);
    }


    /**
     * User: Simony
     * Function:发布项目
     */
    public function publishProgram()
    {
        $memberId = $this->memberId;
        $category_id = input('category_id/d', 0);
        $comment_status = input('comment_status/d');
        $same_sex_status = input('same_sex_status/d');
        $expect_object = input('expect_object/s', 0);
        $date = input('date/s', 0);
        $date_type = input('date_type/d');
//        $begin_date = input('begin_date/d', 0);
//        $end_date = input('end_date/d', 0);
        $content = input('content/s', '');
        $location = input('location/s', '');
        $imgs = input('imgs/s');


        $choose_list = config('app.choose_list');

        if (!isset($choose_list[$date_type])) $this->api_ajaxReturn(ERROR, '时间类型');
        $begin_date = strtotime($date . ' ' . $choose_list[$date_type]['begin_date']);
        $end_date = strtotime($date . ' ' . $choose_list[$date_type]['end_date']);

        $current_time = time();
        $data = [
            'type' => 2,
            'category_id' => $category_id,
            'comment_status' => $comment_status,
            'same_sex_status' => $same_sex_status,
            'expect_object' => $expect_object,
            'begin_date' => $begin_date,
            'end_date' => $end_date,
            'content' => $content,
            'member_id' => $memberId,
            'create_time' => $current_time,
            'location' => $location,
            'date_type' => $date_type,
        ];


        Db::startTrans();
        $res = model('common/content/ContentLogic', 'logic')->publishContent($memberId, $data, $same_sex_status, 2);
        if ($res['code'] != SUCCESS) {
            Db::rollback();
            $this->api_ajaxReturn($res['code'], $res['msg'], $res['result']);
        }


        $dataAll = [];
        $img_arr = $imgs ? explode(',', $imgs) : [];
       if($img_arr){
           foreach ($img_arr as $v) {
               $dataAll[] = [
                   'member_id' => $memberId,
                   'table_id' => $res['result']['id'],
                   'type' => 5,
                   'status' => 1,
                   'url' => $v,
                   'create_time' => $current_time,
               ];
           }
           $add_res = model('common/member/File')->addAll($dataAll);
           if (!$add_res) {
               Db::rollback();
               $this->api_ajaxReturn(ERROR, '图片上传异常');
           }
       }


        Db::commit();
        $this->api_ajaxReturn($res['code'], $res['msg'], $res['result']);
    }


    /**
     * User: Simony
     * Function:点赞
     */
    public function like()
    {
        $memberId = $this->memberId;
        $id = input('post.id/d', 0);
        if (empty($id)) $this->api_ajaxReturn(ERROR, '参数有误');

        Db::startTrans();
        $res = model('common/content/ContentLogic', 'logic')->like($id, $memberId);
        $res['code'] == SUCCESS ? Db::commit() : Db::rollback();
        $this->api_ajaxReturn($res['code'], $res['msg'], $res['result']);
    }


    /**
     * User: Simony
     * Function: 回复内容 （评论+报名）
     */
    public function replyContent()
    {
        $memberId = $this->memberId;
        $content_id = input('post.content_id/d');
        $url_list = input('post.url_list/s');
        //params
        $data = [
            'pid' => input('post.pid/d', 0),
            'content' => input('post.content/s'),
            'type' => input('post.type/d', 1),
        ];


        Db::startTrans();
        $res = model('common/content/ContentLogic', 'logic')->replyContent($memberId, $content_id, $data, $url_list);
        if ($res['code'] == SUCCESS) {
            $res['result'] = array_merge($res['result'], [
                'avatar' => $this->member['avatar'],
                'nickname' => $this->member['nickname'],
                'sex' => $this->member['sex']
            ]);
            Db::commit();
        } else {
            Db::rollback();
        }

        $this->api_ajaxReturn($res['code'], $res['msg'], $res['result']);
    }


    /**
     * User: Simony
     * Function:举报内容
     */
    public function reportContent()
    {
        $memberId = $this->memberId;

        //params
        $data = [
            'content_id' => input('post.content_id/d'),
            'desc' => input('post.desc/s'),
            'type_id' => input('post.type_id/d'),
        ];

        $valdate = validate('mobileapp/reportUser');
        if (!$valdate->check($data, [], 'reportContent')) $this->api_ajaxReturn(ERROR, $valdate->getError());

        Db::startTrans();
        $res = model('common/content/ContentLogic', 'logic')->reportContent($memberId, $data['content_id'], $data['type_id'], $data['desc']);
        $res['code'] == SUCCESS ? Db::commit() : Db::rollback();
        $this->api_ajaxReturn($res['code'], $res['msg'], $res['result']);
    }

    /**
     * User: Simony
     * Function:举报类型列表
     */
    public function getReportTypeList()
    {
        $list = model('common/report/ReportType')->getList([], 0, 'id,name', 'sort desc');
        $this->api_ajaxReturn(SUCCESS, '获取成功', $list);
    }


    /**
     * User: Simony
     * Function:内容列表
     */
    public function getContentList()
    {
        $memberId = $this->memberId;

        $type = input('post.type/d', 2);
        $category_id = input('post.category_id/d', 0);
        $location = input('post.location/s', '');
        $page = input('post.per_page/d', 10);
        $appoint_member_id = input('post.appoint_member_id/d', -1);

        $res = model('common/content/ContentLogic', 'logic')->getContentList($memberId, $page, $type, $category_id, $location, $appoint_member_id);
        $this->api_ajaxReturn($res['code'], $res['msg'], $res['result']);
    }

    /**
     * User: Simony
     * Function: 内容详细
     */
    public function getContentDetail()
    {
        $memberId = $this->memberId;
        $contend_id = input('post.content_id/d');

        $res = model('common/content/ContentLogic', 'logic')->getContentDetail($memberId, $contend_id);
        $this->api_ajaxReturn($res['code'], $res['msg'], $res['result']);
    }

    /**
     * User: Simony
     * Function:报名 （弃用）
     */
    public function signUp()
    {
        $memberId = $this->memberId;
        $contend_id = input('post.content_id/d');

        Db::startTrans();
        $res = model('common/content/ContentLogic', 'logic')->signUp($memberId, $contend_id);
        $res['code'] == SUCCESS ? Db::commit() : Db::rollback();
        $this->api_ajaxReturn($res['code'], $res['msg'], $res['result']);
    }

    /**
     * User: Simony
     * Function:我的内容(动态+项目)
     */
    public function myContent()
    {
        $memberId = $this->memberId;

        $type = input('post.type/d', 2);
        $page = input('post.per_page/d', 10);

        $res = model('common/content/ContentLogic', 'logic')->myContent($memberId, $page, $type);
        $this->api_ajaxReturn($res['code'], $res['msg'], $res['result']);
    }

    /**
     * User: Simony
     * Function:删除评论
     */
    public function delComment()
    {
        $memberId = $this->memberId;
        $id = input('post.id/d', 0);

        Db::startTrans();
        $res = model('common/content/ContentLogic', 'logic')->delComment($memberId, $id);
        $res['code'] == SUCCESS ? Db::commit() : Db::rollback();

        $this->api_ajaxReturn($res['code'], $res['msg'], $res['result']);

    }


    /**
     * User: Simony
     * Function: 协议列表
     */
    public function agreementList()
    {
        $agreementModel = model('common/agreement/Agreement');
        $field = 'id,page_title';
        $data = $agreementModel->getList([], 0, $field, 'sort desc');
        $this->api_ajaxReturn(SUCCESS, '获取成功', $data);
    }

    /**
     * User: Simony
     * Function:协议明细
     */
    public function agreement()
    {
        $id = input('param.id/d', 0);
        $agreementModel = model('common/agreement/Agreement');
        $field = 'id,page_title,title,content,create_time';
        $agreement = $agreementModel->getInfoByID($id, $field);
        if (empty($agreement)) $this->api_ajaxReturn(ERROR, '协议不存在');
        $agreement['content'] = htmlspecialchars_decode($agreement['content']);
        $agreement['date'] = date('Y-m-d H:i:s', $agreement['create_time']);
        unset($agreement['create_time']);
        $this->api_ajaxReturn(SUCCESS, '获取成功', $agreement);
    }
}
